package com.hm.manage.pach;

import com.alibaba.fastjson2.JSONObject;
import com.hm.manage.domain.Shuj;
import com.hm.manage.service.IShujService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;


@Slf4j
@Component
public class PostExample {

    String Cookie = "JSESSIONID=3F8873EF63A36DFC6A2C190865B6F106";
    String token = "d10d76ee8686472991093a8e97fd391c";

    @Autowired
    private IShujService shujService;

    public int getPages(){
        try {
            // 0、先获取鉴权信息
            Shuj shuj = shujService.getById(1);
            Cookie = shuj.getCcookie();
            token = shuj.getTtoken();

            // 1. 定义目标 URL
            URL url = new URL("http://psvs.szpt.ga/api/systemArchive/queryPage");

            // 2. 打开连接并强转为 HttpURLConnection
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            // 3. 设置请求方法为 POST
            conn.setRequestMethod("POST");

            // 4. 允许输出流（发送请求体）
            conn.setDoOutput(true);

            // 5. 设置请求头（表单数据）
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Cookie", Cookie);
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
            conn.setRequestProperty("x-access-token",token);

            // 6. 准备请求参数并进行 URL 编码
            String json = "{\"pageNum\": "+1+", \"pageSize\": "+10+"}";

            // 7. 发送请求参数
            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = json.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

            // 8. 获取响应码
            int status = conn.getResponseCode();
            System.out.println("响应状态码: " + status);

            // 9. 读取响应内容
            StringBuilder response = new StringBuilder();
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            conn.getInputStream(),
                            StandardCharsets.UTF_8
                    )
            )) {
                String line;
                while ((line = br.readLine()) != null) {
                    response.append(line);
                }
            }

            System.out.println("响应内容: " + response.toString());
            JSONObject root = JSONObject.parseObject(response.toString());
            JSONObject data = root.getJSONObject("data");
            Integer pages = data.getInteger("pages");
            return pages;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public String getList(int pageNum,int pageSize) {
        try {
            // 0、先获取鉴权信息
            Shuj shuj = shujService.getById(1);
            Cookie = shuj.getCcookie();
            token = shuj.getTtoken();

            // 1. 定义目标 URL
            URL url = new URL("http://psvs.szpt.ga/api/systemArchive/queryPage");

            // 2. 打开连接并强转为 HttpURLConnection
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            // 3. 设置请求方法为 POST
            conn.setRequestMethod("POST");

            // 4. 允许输出流（发送请求体）
            conn.setDoOutput(true);

            // 5. 设置请求头（表单数据）
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
            conn.setRequestProperty("Cookie", Cookie);
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
            conn.setRequestProperty("x-access-token",token);

            // 6. 准备请求参数并进行 URL 编码
           String json = "{\"pageNum\": "+pageNum+", \"pageSize\": "+pageSize+"}";

            // 7. 发送请求参数
            try (OutputStream os = conn.getOutputStream()) {
                byte[] input = json.getBytes(StandardCharsets.UTF_8);
                os.write(input, 0, input.length);
            }

            // 8. 获取响应码
            int status = conn.getResponseCode();
            System.out.println("响应状态码: " + status);

            // 9. 读取响应内容
            StringBuilder response = new StringBuilder();
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            conn.getInputStream(),
                            StandardCharsets.UTF_8
                    )
            )) {
                String line;
                while ((line = br.readLine()) != null) {
                    response.append(line);
                }
            }

            System.out.println("响应内容: " + response.toString());
            return response.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }


    public String getDetail(String archiveCode) {
        try {
            // 1. 定义目标 URL  http://psvs.szpt.ga/api/systemArchive/archiveDetail/320412010001202504281652480000001
            String baseUrl = "http://psvs.szpt.ga/api/systemArchive/archiveDetail/"+archiveCode;
            log.info(baseUrl);
            URL url = new URL(baseUrl);

            // 2. 打开连接并强转为 HttpURLConnection
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            // 3. 设置请求方法为 POST
            conn.setRequestMethod("POST");

            // 5. 设置请求头（表单数据）
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json, text/plain, */*");
            conn.setRequestProperty("Cookie", "JSESSIONID=A8E9AFAC681B87769287198440A55D08");
            conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36");
            conn.setRequestProperty("x-access-token","00a8d4f653f44800ae7959b490a44ed6");
            conn.setRequestProperty("Accept-Encoding","gzip, deflate");


            // 8. 获取响应码
            int status = conn.getResponseCode();
            System.out.println("响应状态码: " + status);

            // 9. 读取响应内容
            StringBuilder response = new StringBuilder();
            try (BufferedReader br = new BufferedReader(
                    new InputStreamReader(
                            conn.getInputStream(),
                            StandardCharsets.UTF_8
                    )
            )) {
                String line;
                while ((line = br.readLine()) != null) {
                    response.append(line);
                }
            }

            System.out.println("响应内容: " + response.toString());
            return response.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }




}
